import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.transaction.UserTransaction;

import br.ufpr.estoque.ServicoEstoque;
import br.ufpr.venda.ServicoVenda;


public class TestarModulosEJB {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		UserTransaction utx=null;
		
		try {
			Properties p = new Properties();
			configura(p);
			int falhas=0, execucoes=0;
			
			for(int i=0; i<40; i++) {
				execucoes++;
			try {
				utx = (UserTransaction)new InitialContext(p).lookup("java:comp/UserTransaction");
				utx.begin();
			
			ServicoVenda sV = (ServicoVenda)new InitialContext(p).lookup("servicoVenda");
			ServicoEstoque sE = (ServicoEstoque)new InitialContext(p).lookup("servicoEstoque");
			
			sV.insereVenda();
			sE.diminuiEstoque();
			
			utx.commit();
			System.out.println("Essa funcionou");
			System.out.flush();

			}catch(Exception e) {
				falhas++;
				System.out.println("Essa falhou: " + e.getMessage());
				if (utx != null) {
					try {
						utx.rollback();
					}catch(Exception f) {
						f.printStackTrace();
					}
				}
			}
			utx=null;
			
			}
			
			System.out.println("Estatistica da execucao");
			System.out.println("- " + execucoes + " execucoes;");
			System.out.println("- " + falhas + " falhas;");
			
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		

	}

	private static void configura(Properties props) {
		props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory");
		props.setProperty(Context.URL_PKG_PREFIXES, "com.sun.enterprise.naming");
		props.setProperty(Context.STATE_FACTORIES, "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
		// servidor
		props.setProperty("org.omg.CORBA.ORBInitialHost", "192.168.0.100");
		// porta do servidor
		props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
		
	}
 	
}
